package com.rmb.finance.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.cost.service.CostRechargeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rmb.common.exception.ExceptionCast;
import com.rmb.common.model.response.CommonCode;
import com.rmb.finance.dao.RmbFinanceOrderDao;
import com.rmb.finance.service.RmbFinanceOrderService;
import com.rmb.mart.entity.cost.CostRecharge;
import com.rmb.mart.entity.linxi.RmbFinanceOrder;
import com.rmb.mart.entity.linxi.util.StringUtils;
import com.rmb.mart.entity.linxi.vo.Invoice;
import com.rmb.mart.entity.linxi.vo.OrderExcel;
import com.rmb.mart.entity.linxi.vo.RmbFinanceOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
 * (RmbFinanceOrder)表服务实现类
 *
 * @author makejava
 * @since 2021-01-23 08:44:23
 */
@Service("rmbFinanceOrderService")
public class RmbFinanceOrderServiceImpl implements RmbFinanceOrderService {
    @Resource
    private RmbFinanceOrderDao rmbFinanceOrderDao;

    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String username;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RmbFinanceOrder queryById(Long id) {
        return this.rmbFinanceOrderDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<RmbFinanceOrder> queryAllByLimit(int offset, int limit) {
        return this.rmbFinanceOrderDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param rmbFinanceOrder 实例对象
     * @return 实例对象
     */
    @Override
    public RmbFinanceOrder insert(RmbFinanceOrder rmbFinanceOrder) {
        this.rmbFinanceOrderDao.insert(rmbFinanceOrder);
        return rmbFinanceOrder;
    }

    /**
     * 修改数据
     *
     * @param rmbFinanceOrder 实例对象
     * @return 实例对象
     */
    @Override
    public RmbFinanceOrder update(RmbFinanceOrder rmbFinanceOrder) {
        this.rmbFinanceOrderDao.update(rmbFinanceOrder);
        return this.queryById(rmbFinanceOrder.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.rmbFinanceOrderDao.deleteById(id) > 0;
    }

    /**
     * 查询订单
     * @param rmbFinanceOrderVo 分页数据
     * @return 列表
     */
    @Override
    public PageInfo<Map<String, Object>> findList(RmbFinanceOrderVo rmbFinanceOrderVo) {
        PageHelper.startPage(rmbFinanceOrderVo.getPage(),rmbFinanceOrderVo.getSize());
        List<Map<String, Object>> list = rmbFinanceOrderDao.findByOrder(rmbFinanceOrderVo);
        return new PageInfo<>(list);
    }

    /**
     * 发票审核
     * @param page 起始
     * @param size 条数
     * @return 列表
     */
    @Override
    public PageInfo<Invoice> invoiceList(int page, int size) {
        PageHelper.startPage(page,size);
        return new PageInfo<>(rmbFinanceOrderDao.invoiceList());
    }

    /**
     * 根据充值编号查询订单
     * @param rechargeId 编号
     * @return 列表
     */
    @Override
    public List<Map<String, Object>> findByOrderId(Long rechargeId) {
        return rmbFinanceOrderDao.findByOrderId(rechargeId);
    }

    /**
     * 审核订单
     * @param id 编号
     * @param checkStatus 状态
     * @return 是否
     */
    @Override
    public int updateStatus(Long id,int checkStatus) {
        return rmbFinanceOrderDao.updateStatus(id,checkStatus);
    }

    /**
     * 开票
     * @param id 编号
     * @param invoiceStatus invoiceStatus
     * @return 是否
     */
    @Override
    public int updInvoiceStatus(Long id, int invoiceStatus) {
        Invoice invoiceId = rmbFinanceOrderDao.findByInvoiceId(id);
        if(invoiceStatus == 1){
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(username);
            message.setTo(invoiceId.getAccountEmail());
            message.setSubject("票据");
            message.setText(invoiceId.toString());
            mailSender.send(message);
        }else{
            return rmbFinanceOrderDao.updInvoiceStatus(id,invoiceStatus);
        }
        return rmbFinanceOrderDao.updInvoiceStatus(id,invoiceStatus);
    }

    /**
     * 到处excel
     * @return 数据
     */
    @Override
    public List<OrderExcel> export() {
        List<OrderExcel> list = rmbFinanceOrderDao.export();
        String sub = UUID.randomUUID().toString().substring(0, 4);
        String xlsx = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("xlsx")).toString();
        System.out.println(xlsx);
        String replace = xlsx.replace("file:/", "");
        System.out.println(replace);
        ExcelWriterBuilder write = EasyExcel.write(replace+"/"+sub+".xlsx", OrderExcel.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(list);
        return list;
    }
}